﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ERPForms
{
    public partial class ParteneriSearchFrm : FrmRezumat
    {

        public String Filtru = String.Empty;
        public Boolean bCauta = true;
        public ParteneriSearchFrm(ModuleClass mc)
            : base(mc)
        {
            InitializeComponent();
            workConnString = mc.workConnectionString;
            ActiveControl = cuiTB;
        }

        private void DocumenteSearchFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.parteneriTableAdapter.Connection = con;
            this.parteneriTableAdapter.Fill(this.eSRPDataSetParteneri.Parteneri);

            //adauga aici populare tabele
            InchideConexiune(con);
            AddButton("Filtreaza[F9]", global::ERPStudio.Properties.Resources._001_371, Cauta, ToolStripItemAlignment.Right, false);
            //AddButton("Filtreaza", global::ERPStudio.Properties.Resources._001_38, Filtreaza, ToolStripItemAlignment.Right, false);
            HandleSalvare += Cauta;
            cuiTB.Select(0, cuiTB.Value.ToString().Length);
        }      

        new private void Cauta(object sender, EventArgs e)
        {

            //Filtru = " CodUnitate=" + ModuleInfo.CodUnitate.ToString();

            /*if ((incasariCB.Checked && !platiCB.Checked) || (!incasariCB.Checked && platiCB.Checked))
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru = "TipOperatiune = " + (incasariCB.Checked?"O":"I");
            }

            if (dataRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru = "Data='" + laDataDTP.Value.Year.ToString() + "-" + laDataDTP.Value.Month.ToString() + "-" +
                    laDataDTP.Value.Day.ToString() + "'";
            }
            if (dateRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";

                Filtru = "Data>='" + deLaDataDTP.Value.Year.ToString() + "-" + deLaDataDTP.Value.Month.ToString() + "-" +
                    deLaDataDTP.Value.Day.ToString() + "' and Data<='" + panaLaDataDTP.Value.Year.ToString() + "-" + panaLaDataDTP.Value.Month.ToString() + "-" +
                    panaLaDataDTP.Value.Day.ToString() + "'";
            }
            if (partenerRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDPartener=" + partenerCB.SelectedValue.ToString();
            }

            if (parteneriRB.Checked)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " IDPartener in (";
                String filt = String.Empty;
                for (Int16 i = 0; i < furnizoriArticoleSelectateBindingSource.Count; i++)
                {
                    if (filt != String.Empty)
                        filt += ",";
                    filt += ((FurnizoriArticoleSelectate)furnizoriArticoleSelectateBindingSource.List[i]).Tag.ToString();
                }
                Filtru += filt + ")";
            }
            */
            
            if (cuiTB.Text != String.Empty && cuiTB.Value!=Decimal.Zero)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " CUI = " + cuiTB.Text ;
            }
            if (denumireTB.Text != String.Empty)
            {
                if (Filtru != String.Empty)
                    Filtru += " AND ";
                Filtru += " Denumire like '%" + denumireTB.Text + "%'";
            }

            if (Filtru != String.Empty)
            {
                this.parteneriTableAdapter.Connection = DeschideConexiune(workConnString);
                this.parteneriTableAdapter.FillByFiltru(eSRPDataSetParteneri.Parteneri, Filtru);
                InchideConexiune(this.parteneriTableAdapter.Connection);
                if (parteneriBS.Count == 0)
                {
                    MessageBox.Show("Nu s-a gasit nici o inregistrare care sa satisfaca criteriile selectate!",
                           "Atentie",
                           MessageBoxButtons.OK,
                           MessageBoxIcon.Hand);
                    return;
                }
            }
            
            this.DialogResult = DialogResult.OK;
            Close();

        }

        private void Filtreaza(object sender, EventArgs e)
        {
            bCauta = false;
            Cauta(sender,e);
        }
    }

    [Serializable]
    public class FurnizoriArticoleSelectate
    {
        public FurnizoriArticoleSelectate(String nume, Int16 tag)
        {
            _Name = nume;
            _Tag = tag;
        }

        private String _Name = String.Empty;
        [System.ComponentModel.Bindable(true)]
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private Int16 _Tag = 0;
        [System.ComponentModel.Bindable(true)]
        public Int16 Tag
        {
            get
            {
                return _Tag;
            }
            set
            {
                _Tag = value;
            }
        }
    }
}
